<template>
  <div id="user">
    <nav-bar>
      <template v-slot:default>用户中心</template>
    </nav-bar>
    <div class="logout">
      <van-button round type="info" color="#44b883" @click="tologout">
        退出登录
      </van-button>
    </div>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import { logout } from "network/user";
import { useRouter } from "vue-router";
import { Toast } from "vant";
import { useStore } from "vuex";

export default {
  name: "Profile",
  setup() {
    const store = useStore;
    const router = useRouter();
    const tologout = () => {
      logout().then((res) => {
        if (res.status == "204") {
          Toast.success("退出成功");
          // 清除token
          window.localStorage.setItem("token", "");
          // 在vuex isLogin
          store.commit("setIsLogin", true);
          setTimeout(() => {
            router.push({ path: "/login" });
          }, 300);
        }
      });
    };
    return { tologout };
  },
  components: {
    NavBar,
  },
};
</script>

<style scoped lang='scss'>
#user {
  .logout {
    margin-top: 100px;
  }
}
</style>